﻿using System;
using System.Collections.Generic;
using System.Text;
using Emerson.Storage.Agent.DeviceAccess;
using Emerson.Storage.Agent.Core;
using BPCommLib;

namespace PowervarAccess
{
    public class PowervarBatteryAccess : IBatteryAccess, IDisposable
    {
        SerialAPI api = new SerialAPI();
        BatteryInfo batteryInfo = new BatteryInfo();

        private bool disposed = false;

        public PowervarBatteryAccess()
        {
            api.OpenSerial();
        }

        BatteryInfo IBatteryAccess.GetBatteryInfo()
        {
            batteryInfo.BatteryType = "Powervar";

            try
            {
                batteryInfo.Chemistry = Chemistry();
                batteryInfo.PercentRemaining = PercentRemaining();
                batteryInfo.BatteryChargingState = BatteryChargingState();
                batteryInfo.Temperature = Temperature();
                batteryInfo.IsConnected = api.IsConnected;

                if (batteryInfo.IsConnected)
                    batteryInfo.LastConnectedTime = DateTime.Now;
            }
            catch (Exception)
            {
                batteryInfo.IsConnected = false;
            }

            return batteryInfo;
        }

        private double Temperature()
        {
            double temperature = api.GetParameterValue(
                  SerialAPI.BatteryParameters.P47_Heatsink_Temperature);

            return temperature;
        }

        private BatteryChargingState BatteryChargingState()
        {
            return (BatteryChargingState)api.DeviceStatus;
        }

        private double PercentRemaining()
        {
            double toppingChargeTime = api.GetParameterValue(
                 SerialAPI.BatteryParameters.P30_Topping_Charge_Time);
            double batCapacity = api.GetParameterValue(SerialAPI.BatteryParameters.P63_Battery_Capacity);
            double chargeState = api.GetParameterValue(SerialAPI.BatteryParameters.P32_Charge_State);
            double batCapacityFmt = batCapacity;
            double remainingBatteryCapacity_ToppingStart = 0;
            double batteryCapacity_QuickCharge = 0;

            if (chargeState == 9)
            {
                if (batCapacity < 100)
                {
                    remainingBatteryCapacity_ToppingStart = (100 - batCapacity) / 0.75;
                    batteryCapacity_QuickCharge = batCapacity;

                    batCapacityFmt = batteryCapacity_QuickCharge +
                        (remainingBatteryCapacity_ToppingStart * (toppingChargeTime / 3600));
                }
            }

            return batCapacityFmt;
        }

        private string Chemistry()
        {
            return "--";
        }

        #region for Disposable design pattern

        public void Dispose()
        {
            if (!disposed)
            {
                api.CloseSerial();
                disposed = true;
            }
        }

        ~PowervarBatteryAccess()
        {
            Dispose();
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
